package cn.tedu.tmall.admin.mall.service.impl;

import cn.tedu.tmall.admin.mall.dao.persist.repository.ICategoryRepository;
import cn.tedu.tmall.admin.mall.pojo.entity.Category;
import cn.tedu.tmall.admin.mall.pojo.param.CategoryAddNewParam;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryListItemVO;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryStandardVO;
import cn.tedu.tmall.admin.mall.service.ICategoryService;
import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.vo.PageData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

@Service
public class CategoryServiceImpl implements ICategoryService {

    @Value("${tmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private ICategoryRepository categoryRepository;

    @Override
    public void addNew(CategoryAddNewParam categoryAddNewParam) {
        String name = categoryAddNewParam.getName();
        int count = categoryRepository.countByName(name);
        if (count > 0) {
            String message = "添加类别失败，类别名称已经被占用！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        Integer depth = 1;
        Long parentId = categoryAddNewParam.getParentId();
        CategoryStandardVO parentCategory = null;
        if (parentId != 0) {
            parentCategory = categoryRepository.getStandardById(parentId);
            if (parentCategory == null) {
                String message = "添加类别失败，父级类别不存在！";
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
            }
            depth = parentCategory.getDepth() + 1;
        }

        Category category = new Category();
        BeanUtils.copyProperties(categoryAddNewParam, category);
        category.setDepth(depth);
        category.setIsParent(0);
        int rows = categoryRepository.insert(category);
        if (rows != 1) {
            String message = "添加类别失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }

        if (parentCategory != null && parentCategory.getIsParent() == 0) {
            Category updateCategory = new Category();
            updateCategory.setId(parentId);
            updateCategory.setIsParent(1);
            rows = categoryRepository.updateById(updateCategory);
            if (rows != 1) {
                String message = "添加类别失败，服务器忙，请稍后再试！";
                throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
            }
        }
    }

    @Override
    public void deleteById(Long id) {
        // TODO 检查类别是否存在
        // TODO 检查是否为父级类别
        // TODO 检查是否与其它数据存在关联关系

        int rows = categoryRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除类别失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }

        // TODO 如果这不是一级类别，且是父级中的最后一个子级，则需要将父级类别的isParent改为0
    }

    @Override
    public void setEnable(Long id) {
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id, 0);
    }

    @Override
    public PageData<CategoryListItemVO> listByParent(Long parentId, Integer pageNum) {
        return categoryRepository.listByParent(parentId, pageNum, defaultQueryPageSize);
    }

    @Override
    public PageData<CategoryListItemVO> listByParent(Long parentId, Integer pageNum, Integer pageSize) {
        return categoryRepository.listByParent(parentId, pageNum, pageSize);
    }

    private void updateEnableById(Long id, Integer enable) {
        // TODO 检查类别是否存在
        // 如果不存在，则：String message = xx[enable] + "类别失败，尝试访问的数据不存在！";

        Category category = new Category();
        category.setId(id);
        category.setEnable(enable);
        int rows = categoryRepository.updateById(category);
    }

}